package com.ply.flashsalessystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.entity.pojo.OrderForGoodsDetaile;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.vo.manage.SortVo;
import com.ply.flashsalessystem.entity.wrapper.SortWrapper;
import com.ply.flashsalessystem.service.OrderForGoodsDetaileService;
import com.ply.flashsalessystem.service.OrderForGoodsService;
import com.ply.flashsalessystem.service.SortService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/sort")
@Api("分拣 接口")
@CrossOrigin
public class SortController {

    @Autowired
    OrderForGoodsDetaileService orderForGoodsDetaileService;
    @Autowired
    SortService sortService;

    /*
   分拣
    */
    @AuthorityName(authorityId = 42, authorityName = "分拣查询")
    @ApiOperation("分拣查询")
    @GetMapping(value = {"getSort/{current}/{limit}/{goodsId}/{nums}", "/getSort/{goodsId}/{nums}"})
    public Result getSort(@PathVariable(value = "current", required = false) Integer current,
                          @PathVariable(value = "limit", required = false) Integer limit,
                          @PathVariable("goodsId") Integer goodsId,
                          @PathVariable("nums") double nums) {
        // 需要优化 没有这个 id 错误  nums 错误
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        if (nums <= 0) {
            return Result.error().message("数量错误");
        }
        // 优化: 查询出来 这个商品 总共有多少 不比较数量的  并且把最大数量  显示到前台
        Page<OrderForGoodsDetaile> page = orderForGoodsDetaileService.queryNumsForSort(current, limit, goodsId, nums);
        return Result.ok().message("查询分拣成功").data("total", page.getTotal()).data("rows", page.getRecords());
    }
    // 执行分拣

    /**
     * @param orgDId    订单详情id
     * @param jrId      入库id
     * @param goodsNums 商品数量
     */
    @AuthorityName(authorityId = 43, authorityName = "订单分拣")
    @ApiOperation("订单分拣  订单详情id 入库id 商品数量")
    @PostMapping("doSort/{orgDId}/{jrId}/{goodsNums}")
    public Result doSort(@PathVariable("orgDId") Integer orgDId,
                         @PathVariable("jrId") Integer jrId,
                         @PathVariable("goodsNums") double goodsNums) {
        return sortService.sort(orgDId, jrId, goodsNums)
                ? Result.ok().message("分拣成功")
                : Result.error().message("分拣失败");
    }

    @AuthorityName(authorityId = 44, authorityName = "查询分拣记录")
    @ApiOperation("查询分拣记录")
    @PostMapping(value = {"querySort/{current}/{limit}", "querySort"})
    public Result querySort(@PathVariable(value = "current", required = false) Integer current,
                            @PathVariable(value = "limit", required = false) Integer limit,
                            @RequestBody SortWrapper sortWrapper) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }

        if (sortWrapper == null) {
            sortWrapper = new SortWrapper();
        }

        Page<SortVo> page = sortService.querySort(current, limit, sortWrapper);
        return Result.ok().message("查询成功")
                .data("total", page.getTotal())
                .data("rows", page.getRecords());
    }
}

